<?php
include './include/common.inc.php';
include '../admin/include/keywords.class.php';

$k = new keywords;
$action = isset($action) ? $action : 'manage';
$forward = isset($forward) ? $forward : (HTTP_REFERER ? HTTP_REFERER : $self_url);

if(check_perms('rkeywords')===false)
{
    showmessage('您没有权限进行此操作！');
}

switch ($action)
{

    case 'add' :

        if(isset($submitform))
        {
            if(!$k->add($data))
            {
                $k->error || $response->alert("添加失败");
            }
            else
            {
                $response->alert("添加成功");
                $response->redirect($forward);
            }
            $response->execute();
        }
        else
        {
            include template('admin', 'keywords_add');
        }
        break;

    case 'edit' :

        $id = isset($id) ? $id : 0;
        if(isset($submitform))
        {
            if(!$k->edit($id, $data))
            {
                $k->error || $response->alert("修改失败");
            }
            else
            {
                $response->alert("修改成功");
                $response->redirect($forward);
            }
            $response->execute();
        }
        else
        {
            $data = $k->get_one($id);
            include template('admin', 'keywords_add');
        }
        break;

    case 'delete' :

        if(!isset($id))
        {
            $response->alert("您要删除的关键字不存在");
        }
        if(!$k->delete($id))
        {
            $response->alert("删除失败");
        }
        else
        {
            $response->alert("删除成功");
            $response->script("self.location=self.location");
        }
        $response->execute();
        break;

    case 'delete_all' :

        if(!is_array($selected))
        {
            $response->alert("请选择要删除的关键字");
            $response->real_execute();
        }
        if(!$k->delete_all($selected))
        {
            $response->alert("删除失败");
        }
        else
        {
            $response->alert("删除成功");
            $response->script("self.location=self.location");
        }
        $response->execute();
        break;
    
	default:

        $page = isset($page) ? $page : 1;
        $word = isset($word) ? $word : '';
        $data = $k->listinfo($word, $page);
        $pages = $k->pages;
		include template('admin', 'keywords_manage');
}
?>